<template>
  <layout-body>
    <div>
      <el-button
        type="primary"
        v-permission="'store-shop-create'"
        @click="createClick(0)"
      >
        <el-icon><Plus /></el-icon>
        添加门店
      </el-button>
    </div>
    <!-- 列表 -->
    <el-table v-loading="initLoading" class="mt20" :data="lists" row-key="id">
      <el-table-column label="logo" prop="logo">
        <template #default="scope">
          <el-image
            style="width: 30px; height: 30px"
            :preview-src-list="[scope.row.shop_logo]"
            preview-teleported
            :src="scope.row.shop_logo"
            fit="cover"
          />
        </template>
      </el-table-column>
      <el-table-column label="门店名称" prop="shop_name">
        <template #default="scope">
          <span>{{ scope.row.shop_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="营业时间" prop="start_time">
        <template #default="scope">
          <span>{{ scope.row.start_time }}/{{ scope.row.end_time }}</span>
        </template>
      </el-table-column>
      <el-table-column label="联系电话" prop="phone">
        <template #default="scope">
          <span>{{ scope.row.phone }}</span>
        </template>
      </el-table-column>
      <el-table-column label="门店地址" prop="full_address">
        <template #default="scope">
          <span>{{ scope.row.full_address }}</span>
        </template>
      </el-table-column>
      <el-table-column label="自提核销" prop="is_check" align="center">
        <template #default="scope">
          <span v-if="scope.row.is_check" class="text-default">支持</span>
          <span v-else class="text-grey">不支持</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <el-switch
            v-model="scope.row.status.value"
            size="small"
            :active-value="1"
            :inactive-value="0"
            :loading="scope.row.loading"
            :before-change="
              () => {
                return (scope.row.switch = true);
              }
            "
            @change="statusChange($event, scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" prop="create_time">
        <template #default="scope">
          <span class="text-grey fz12">{{ scope.row.create_time }}</span>
        </template></el-table-column
      >
      <el-table-column align="left" label="操作" width="120">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="createClick(scope.row.id)"
            v-permission="'store-shop-update'"
            >编辑</el-button
          >

          <el-popconfirm
            confirm-button-text="确定"
            cancel-button-text="不用了"
            icon="WarningFilled"
            icon-color="red"
            title="确定删除吗？"
            @confirm="del(scope.row.id)"
          >
            <template #reference>
              <el-button
                link
                v-permission="'store-shop-delete'"
                type="primary"
                >删除</el-button
              >
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="flex end">
      <page :listPage="listPage" @change="pageChange"></page>
    </div>
  </layout-body>
</template>
<script lang="ts" setup>
import { ref, getCurrentInstance, onMounted } from "vue";
import router from "@/router";
import { delShop, getShopList, updateStatusShop } from "@/api/store/shop";
import { listPageType, Result, ResultError } from "@/types";

import { useConfig } from "@/store";
const configStore = useConfig();

const {
  proxy,
  proxy: { $utils },
} = getCurrentInstance() as any;

const createClick = (id: number) => {
  router.push({
    path: "/store/shop/create",
    query: {
      id,
    },
  });
};

const initLoading = ref<boolean>(false);

const lists = ref<any>();

const toInit = () => {
  initLoading.value = true;
  let obj: any = {};
  obj.page = listPage.value.page;
  obj.limit = listPage.value.limit;
  getShopList(obj)
    .then((res: Result) => {
      lists.value = res.data.data;
      listPage.value.total = res.data.total;
      setTimeout(() => {
        initLoading.value = false;
      }, 300);
    })
    .catch((err: ResultError) => {
      $utils.errorMsg(err);
    });
};

const listPage = ref<listPageType>({
  total: 0,
  limit: configStore.getPageLimit,
  page: 1,
});

const pageChange = (res: listPageType) => {
  listPage.value = res;
  toInit();
};

const statusChange = (
  val: number,
  row: {
    switch: boolean;
    loading: boolean;
    id: number;
  }
) => {
  if (row.switch === true) {
    row.loading = true;
    updateStatusShop({
      id: row.id,
      status: val,
    })
      .then((res: Result) => {
        row.loading = false;
        toInit();
        $utils.successMsg(res);
      })
      .catch((err: ResultError) => {
        row.loading = false;
        toInit();
        $utils.errorMsg(err);
      });
  }
};

const del = (id: number) => {
  delShop({
    id,
  })
    .then((res: Result) => {
      toInit();
      $utils.successMsg(res.message);
    })
    .catch((err: ResultError) => {
      $utils.errorMsg(err);
    });
};

onMounted(() => {
  toInit();
});
</script>